﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleMenu;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleMenu
{
    public class RoleNameQueryCommandHandler : IRequestHandler<RoleNameQueryCommand, APIPanging<Role>>
    {
        private readonly IBaseRepository<Role> rRepo;
        private readonly IMapper mapper;

        public RoleNameQueryCommandHandler(IBaseRepository<Role>rRepo,IMapper mapper)
        {
            this.rRepo = rRepo;
            this.mapper = mapper;
        }

        public async Task<APIPanging<Role>> Handle(RoleNameQueryCommand request, CancellationToken cancellationToken)
        {
            APIPanging<Role> result = new APIPanging<Role>();
            var list = rRepo.Getall().Where(x => x.IsDeleted == false);
            int totalcount = list.Count();
            int totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.Pagesize);
            var model = list.OrderBy(p => p.RoleId).Skip((request.Pageindex - 1) * request.Pagesize).Take(request.Pagesize).ToList();
            result.TotalPage = totalpage;
            result.TotalCount = totalcount;
            result.Code = APIEnum.查询成功;
            result.Msg = "分页查询成功";
            result.List = list.ToList();
            return await Task.FromResult(result);
        }


    


    }
}
